﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC.Nozzle
{
	public partial class FormNozzleCalcCheck : System.Windows.Forms.Form
	{
		public FormNozzleCalcCheck()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this._isFirstInit = true;
			this.ncc = new NozzleCalcCheck();
			this.InitParameterSetting();
			this._isFirstInit = false;
		}

		private void InitParameterSetting()
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			Dictionary<string, string> nozzleCalc = hvacuserSetting.GetNozzleCalc();
			this.ncc.AirSeasonType = Convert.ToInt32(hvacuserSetting.GetValueFromDic("airSeasonType", "0", nozzleCalc));
			this.ncc.ParaL = Convert.ToDouble(hvacuserSetting.GetValueFromDic("paraL", "10000", nozzleCalc));
			this.ncc.Paran = Convert.ToInt32(hvacuserSetting.GetValueFromDic("paran", "8", nozzleCalc));
			this.ncc.Paray = Convert.ToDouble(hvacuserSetting.GetValueFromDic("paray", "4", nozzleCalc));
			this.ncc.Parax = Convert.ToDouble(hvacuserSetting.GetValueFromDic("parax", "12", nozzleCalc));
			this.ncc.Parad = Convert.ToDouble(hvacuserSetting.GetValueFromDic("parad", "0.2", nozzleCalc));
			this.ncc.ParaB = Convert.ToDouble(hvacuserSetting.GetValueFromDic("paraB", "0", nozzleCalc));
			this.ncc.Paraa = Convert.ToDouble(hvacuserSetting.GetValueFromDic("paraa", "0.07", nozzleCalc));
			this.ncc.Parag = Convert.ToDouble(hvacuserSetting.GetValueFromDic("parag", "9.81", nozzleCalc));
			this.ncc.Parats = Convert.ToDouble(hvacuserSetting.GetValueFromDic("parats", "10", nozzleCalc));
			this.ncc.Paratn = Convert.ToDouble(hvacuserSetting.GetValueFromDic("paratn", "26", nozzleCalc));
			this.InitParaControl();
			this.InitCalcControl();
		}

		private void UserSettingWrite()
		{
			new HVACUserSetting().WriteNozzleCalc(new Dictionary<string, string>
			{
				{
					"airSeasonType",
					this.ncc.AirSeasonType.ToString()
				},
				{
					"paraL",
					this.ncc.ParaL.ToString(CultureInfo.InvariantCulture)
				},
				{
					"paran",
					this.ncc.Paran.ToString()
				},
				{
					"paray",
					this.ncc.Paray.ToString(CultureInfo.InvariantCulture)
				},
				{
					"parax",
					this.ncc.Parax.ToString(CultureInfo.InvariantCulture)
				},
				{
					"parad",
					this.ncc.Parad.ToString(CultureInfo.InvariantCulture)
				},
				{
					"paraB",
					this.ncc.ParaB.ToString(CultureInfo.InvariantCulture)
				},
				{
					"paraa",
					this.ncc.Paraa.ToString(CultureInfo.InvariantCulture)
				},
				{
					"parag",
					this.ncc.Parag.ToString(CultureInfo.InvariantCulture)
				},
				{
					"parats",
					this.ncc.Parats.ToString(CultureInfo.InvariantCulture)
				},
				{
					"paratn",
					this.ncc.Paratn.ToString(CultureInfo.InvariantCulture)
				}
			});
		}

		private void InitParaControl()
		{
			switch (this.ncc.AirSeasonType)
			{
			case 0:
				this.radioAirComfort.Checked = true;
				this.radioSummer.Checked = true;
				break;
			case 1:
				this.radioAirComfort.Checked = true;
				this.radioWinter.Checked = true;
				break;
			case 2:
				this.radioAirTechnical.Checked = true;
				this.radioSummer.Checked = true;
				break;
			case 3:
				this.radioAirTechnical.Checked = true;
				this.radioWinter.Checked = true;
				break;
			default:
				this.radioAirComfort.Checked = true;
				this.radioSummer.Checked = true;
				break;
			}
			this.txtParaL.Text = this.ncc.ParaL.ToString(CultureInfo.InvariantCulture);
			this.txtParan.Text = this.ncc.Paran.ToString();
			this.txtParay.Text = this.ncc.Paray.ToString(CultureInfo.InvariantCulture);
			this.txtParax.Text = this.ncc.Parax.ToString(CultureInfo.InvariantCulture);
			this.txtParad.Text = this.ncc.Parad.ToString(CultureInfo.InvariantCulture);
			this.txtParaβ.Text = this.ncc.ParaB.ToString(CultureInfo.InvariantCulture);
			this.txtParaa.Text = this.ncc.Paraa.ToString(CultureInfo.InvariantCulture);
			this.txtParag.Text = this.ncc.Parag.ToString(CultureInfo.InvariantCulture);
			this.txtParats.Text = this.ncc.Parats.ToString(CultureInfo.InvariantCulture);
			this.txtParatn.Text = this.ncc.Paratn.ToString(CultureInfo.InvariantCulture);
		}

		private void InitCalcControl()
		{
			this.ncc.DoCalculate();
			this.txtCalcAr.Text = this.ncc.CalcAr.ToString(CultureInfo.InvariantCulture);
			this.txtCalcVs.Text = this.ncc.CalcVs.ToString(CultureInfo.InvariantCulture);
			this.txtCalcVx.Text = this.ncc.CalcVx.ToString(CultureInfo.InvariantCulture);
			this.txtCalcVp.Text = this.ncc.CalcVp.ToString(CultureInfo.InvariantCulture);
			this.txtCalcLd.Text = this.ncc.CalcLd.ToString(CultureInfo.InvariantCulture);
			this.txtCalcN.Text = this.ncc.CalcN.ToString(CultureInfo.InvariantCulture);
			this.txtActualVs.Text = this.ncc.ActualVs.ToString(CultureInfo.InvariantCulture);
			this.txtActualVx.Text = this.ncc.ActualVx.ToString(CultureInfo.InvariantCulture);
			this.txtActualVp.Text = this.ncc.ActualVp.ToString(CultureInfo.InvariantCulture);
			this.txtActualN.Text = this.ncc.ActualN.ToString();
			this.UpdateVpColor();
		}

		private void UpdateVpColor()
		{
			switch (this.ncc.AirSeasonType)
			{
			case 0:
			case 3:
				this.txtCalcVp.ForeColor = ((this.ncc.CalcVp <= 0.3) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				this.txtActualVp.ForeColor = ((this.ncc.ActualVp <= 0.3) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				return;
			case 1:
				this.txtCalcVp.ForeColor = ((this.ncc.CalcVp <= 0.2) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				this.txtActualVp.ForeColor = ((this.ncc.ActualVp <= 0.2) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				return;
			case 2:
				if (this.ncc.CalcVp >= 0.2 && this.ncc.CalcVp <= 0.5)
				{
					this.txtCalcVp.ForeColor = System.Drawing.Color.Green;
				}
				else
				{
					this.txtCalcVp.ForeColor = System.Drawing.Color.Red;
				}
				if (this.ncc.ActualVp >= 0.2 && this.ncc.ActualVp <= 0.5)
				{
					this.txtActualVp.ForeColor = System.Drawing.Color.Green;
					return;
				}
				this.txtActualVp.ForeColor = System.Drawing.Color.Red;
				return;
			default:
				this.txtCalcVp.ForeColor = ((this.ncc.CalcVp <= 0.3) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				this.txtActualVp.ForeColor = ((this.ncc.ActualVp <= 0.3) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				return;
			}
		}

		public void RefreshTheUi(string strSe, string strD)
		{
			this.txtParax.Text = strSe;
			this.ncc.Parad = Convert.ToDouble(strD);
			this.txtParad.Text = this.ncc.Parad.ToString(CultureInfo.InvariantCulture);
		}

		private void radioAirComfort_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioAirComfort.Checked)
			{
				this.ncc.AirSeasonType = (this.radioSummer.Checked ? 0 : 1);
			}
			else
			{
				this.ncc.AirSeasonType = (this.radioSummer.Checked ? 2 : 3);
			}
			this.UpdateVpColor();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			base.Close();
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			base.Close();
		}

		private void txtParaL_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.ParaL = Convert.ToDouble(this.txtParaL.Text.Trim());
			if (this.ncc.ParaL < 1.0)
			{
				this.txtParaL.Text = "1";
				return;
			}
			this.InitCalcControl();
		}

		private void txtParan_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			string text = this.txtParan.Text.Trim();
			if (text.Contains("."))
			{
				text = text.Replace(".", "");
				this.txtParan.Text = text;
			}
			this.ncc.Paran = Convert.ToInt32(text);
			if (this.ncc.Paran < 1)
			{
				this.txtParan.Text = "1";
				return;
			}
			this.InitCalcControl();
		}

		private void txtParay_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.Paray = Convert.ToDouble(this.txtParay.Text.Trim());
			this.InitCalcControl();
		}

		private void txtParax_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.Parax = Convert.ToDouble(this.txtParax.Text.Trim());
			if (this.ncc.Parax < 1.0)
			{
				this.txtParax.Text = "1";
				return;
			}
			this.InitCalcControl();
		}

		private void txtParad_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.Parad = Convert.ToDouble(this.txtParad.Text.Trim());
			if (this.ncc.Parax < 0.1)
			{
				this.txtParax.Text = "0.1";
				return;
			}
			this.InitCalcControl();
		}

		private void txtParaβ_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.ParaB = Convert.ToDouble(this.txtParaβ.Text.Trim());
			this.InitCalcControl();
		}

		private void txtParaa_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.Paraa = Convert.ToDouble(this.txtParaa.Text.Trim());
			if (this.ncc.Parag > 1.0)
			{
				this.txtParag.Text = "1";
				return;
			}
			this.InitCalcControl();
		}

		private void txtParag_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			if (this.txtParag.Text.Trim() == "0")
			{
				return;
			}
			this.ncc.Parag = Convert.ToDouble(this.txtParag.Text.Trim());
			this.InitCalcControl();
		}

		private void txtParats_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			if (this.txtParats.Text.Trim() == "0")
			{
				return;
			}
			this.ncc.Parats = Convert.ToDouble(this.txtParats.Text.Trim());
			this.InitCalcControl();
		}

		private void txtParatn_TextChanged(object sender, EventArgs e)
		{
			if (this._isFirstInit)
			{
				return;
			}
			this.ncc.Paratn = Convert.ToDouble(this.txtParatn.Text.Trim());
			this.InitCalcControl();
		}

		private void btnGetPara_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Abort;
		}

		private NozzleCalcCheck ncc;

		private readonly bool _isFirstInit;
	}
}
